/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:09
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.repository;

import com.joysuch.wwyt.edu.entity.EduTestPaperQuestion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import javax.transaction.Transactional;
import java.util.List;

/**
 * EduTestPaperQuestionDao
 *
 */
public interface EduTestPaperQuestionDao
        extends JpaRepository<EduTestPaperQuestion, Long>, QuerydslPredicateExecutor<EduTestPaperQuestion> {
    public Page<EduTestPaperQuestion> findAll(Specification<EduTestPaperQuestion> spec, Pageable pageable);

    public EduTestPaperQuestion save(EduTestPaperQuestion bean);

    public void delete(EduTestPaperQuestion bean);

    @Modifying
    @Query("update EduTestPaperQuestion set deleteFlag='1' where testPaperId=?1 and id not in (?2)")
    void clearRemovedItems(Long testPaperId, List<Long> reservedIds);

    @Modifying
    @Query("update EduTestPaperQuestion set deleteFlag='1' where testPaperId=?1")
    void clearRemovedItems(Long testPaperId);

    List<EduTestPaperQuestion> findByTestPaperIdOrderByIndexNoAsc(Long testPaperId);

    List<EduTestPaperQuestion> findByTestPaperIdAndUserIdOrderByIndexNoAsc(Long testPaperId,Long userId);


    List<EduTestPaperQuestion> findByTestPaperIdAndUserIdAndExamIdOrderByIndexNoAsc(Long testPaperId,Long userId,Long examId);



    int countByQuestionId(Long questionId);

    void deleteByTestPaperId(Long id);

    @Modifying
    @Transactional
    @Query(value = "update wwyt_edu_test_paper_question set DELETE_FLAG='1' where  USER_ID is not null  and EXAM_ID=?1 and DELETE_FLAG='0'",nativeQuery = true)
    void deleteByExamId(Long examId);


    List<EduTestPaperQuestion> findByTestPaperId(Long paperId);

    int countByTestPaperId(Long paperId);

    @Query("select e.questionId from EduTestPaperQuestion e where e.testPaperId=?1")
    List<Long> findQidByPaperId(Long paperId);

    @Modifying
    @Query(value = "update wwyt_edu_test_paper_question set DELETE_FLAG='1' where EXAM_ID=?1 and TEST_PAPER_ID=?2 and  USER_ID=?3 ", nativeQuery = true)
    void deleteByExamIdAndTestPaperIdAndUserId(Long id, Long testPaperId, Long currentUserId);

    @Query("select etpq from EduTestPaperQuestion etpq where etpq.deleteFlag='0' and etpq.id in (?1)")
    List<EduTestPaperQuestion> findbyIds(List<Long> ids);

    @Query(value = "select * from wwyt_edu_test_paper_question etpq where etpq.id in (?1)", nativeQuery = true)
    List<EduTestPaperQuestion> findByIdList(List<Long> ids);

}
